package com.tuanzi.loan.web.bean.extratemplate;

import com.tuanzi.loan.business.entity.extratemplate.ExtraTemplate;
import com.tuanzi.loan.business.service.extratemplate.ExtraTemplateService;
import com.tuanzi.loan.business.service.extratemplate.ExtraTemplateSpecialService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.extratemplate.ExtraTemplateRequest;
import com.tuanzi.loan.vo.response.extratemplate.ExtraTemplateResponse;
import com.tuanzi.loan.web.configuration.JSFListingSessionBean;
import com.tuanzi.loan.web.lazy.extratemplate.ExtraTemplateLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.faces.bean.ViewScoped;
import java.util.List;

/**
 * @author caiwanlin
 * @create 2018-10-16 14:54
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class ExtraTemplateListBean extends JSFListingSessionBean<ExtraTemplateRequest,ExtraTemplateResponse> {
    @Autowired
    private ExtraTemplateService extraTemplateService;
    @Autowired
    private ExtraTemplateSpecialService extraTemplateSpecialService;

    @Override
    protected String page() {
        return "/views/family/extratemplate/extra-template-list.xhtml";
    }

    @Override
    protected void initLazyDataModel() {
        $ = new ExtraTemplateRequest();
        lazyDataModel = new ExtraTemplateLazyDataModel($, session, extraTemplateService);
    }

    public void onReset() {
        initLazyDataModel();
    }

    public String onAdd(){
        return redirect("/views/family/extratemplate/info.xhtml");
    }

    public String onSee(Long id){
        return redirect("/views/family/extratemplate/info.xhtml?id="+id);
    }

    public void onDelete(Long id){
        ExtraTemplate one = extraTemplateService.findOne(id);
        extraTemplateService.delete(one);
        extraTemplateSpecialService.deleteAllByTemplateId(one.getTemplateId());
        //删除缓存
        extraTemplateService.deleteExtraTemplateCache(one.getTemplateId());
        addSuccessfullyMessage();
    }

    public void onCopy(Long id){
        ExtraTemplate one = extraTemplateService.findOne(id);
        ExtraTemplate extraTemplate = new ExtraTemplate();
        BeanUtils.copyProperties(one,extraTemplate,"id","templateId","creator",
                "create_time","updator","update_time","version");
        extraTemplateService.saveOrUpdate(extraTemplate);
        addSuccessfullyMessage();
    }

    public void deleteOneTemplateCache(Integer templateId){
        extraTemplateService.deleteExtraTemplateCache(templateId);
        addSuccessfullyMessage();
    }

    public void deleteAllTemplateCache(){
        List<ExtraTemplate> all = extraTemplateService.findAll();
        if (all != null && !all.isEmpty()){
            all.forEach(e->{
                Integer templateId = e.getTemplateId();
                if (templateId != null){
                    extraTemplateService.deleteExtraTemplateCache(templateId);
                }
            });
            addSuccessfullyMessage();
        }
    }

}
